<?php
namespace My\Controller;

use Utuo\BackgroundController;

class SiteLinkController extends BackgroundController {
	
	// 状态
	private $status = array(
			0 => '草稿',
			1 => '发布'
	);
	
	// 栏目id
	private $catId;
	
	// 初始化
	public function _initialize() {
		$this->catId = $catId = I('cat_id', 0, 'intval');
		$this->setControllerTitle('链接管理');
		$this->setActionTitle(array(
				'modify' => '修改链接',
		));
		$this->setActionTitle('list', '链接列表', "cat_id=$catId");
		$this->setActionTitle('add', '添加链接', "cat_id=$catId");
		$this->setActionTitle('recycle', '回收站', "cat_id=$catId");
		$this->setActionTitle('index', '全部链接', "cat_id=$catId");
		$this->setActionNav('index,add,recycle');
		$this->assign('cat_id', $catId);
	}
	
	// 微网站链接内容列表
	public function indexAction() {
		$this->getLists( 0 );
	}
	
	// 回收站
	public function recycleAction() {
		$this->getLists( 1 );
	}
	
	/**
	 * 链接列表
	 * @param unknown $is_deleted
	 */
	public function getLists( $is_deleted ) {
		$d = D("Link");
		$page = $this->getPage();
		$pagesize = 10;
		$condition = array(
				'is_deleted' => $is_deleted,
				'cid' => $this->aid,
				'cat_id' => $this->catId
		);
		$linkLists = $d->where($condition)->limit($pagesize)->page($page)->order('id DESC')->select();
		
		$linkCount = $d->where($condition)->count();
		$this->assign('linkLists', $linkLists);
		$this->showPage($linkCount, $pagesize);
		$this->display();
	}
	
	/**
	 * 添加
	 */
	public function addAction() {
		$this->assign("cid",$this->aid);
		$this->display('save');
	}
	
	/**
	 * 修改
	 */
	public function modifyAction() {
		$id = I('id', 0, 'intval');
		$link = M("Link")->where(array('cid'=>$this->aid, 'id'=>$id))->find();
		$this->assign("link", $link);
		$this->assign("cid", $link['cid']);
		$this->display('save');
	}
	
	/**
	 * 保存
	 */
	public function saveAction() {
		$this->submitPost("link");		
	}
	
	/**
	 * 移除
	 */
	public function removeAction() {
		$this->ajaxRemove("link");
	}
	
	/**
	 * 恢复
	 */
	public function restoreAction() {
		$this->ajaxRestore("link");
	}
	
	/**
	 * 删除
	 */
	public function deleteAction() {
		$this->ajaxDelete("link");
	}
}